package com.soda.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.soda.bean.Fmenu;
import com.soda.bean.Menu;
import com.soda.bean.Role;
import com.soda.bean.User;
import com.soda.dao.FmenuDao;
import com.soda.dao.MenuDao;
import com.soda.dao.RoleDao;
import com.soda.dao.UserDao;
import com.soda.dao.impl.FmenuDaoImpl;
import com.soda.dao.impl.MenuDaoImpl;
import com.soda.dao.impl.RoleDaoImpl;
import com.soda.service.RoleService;
import com.soda.util.MyBatisUtil;
import com.soda.util.PageInfo;
import com.soda.util.SelectRoleInfo;

public class RoleServiceImpl implements RoleService{

	RoleDao rd=new RoleDaoImpl();
	FmenuDao fd=new FmenuDaoImpl();
	MenuDao md=new MenuDaoImpl();
	public List<Role> getAllRole() {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		 List<Role> rolelist=session.getMapper(RoleDao.class).getAllRole();
		return rolelist;
	}
	/*
	 * 通过id查找role
	 * @see com.soda.service.RoleService#getRoleById(java.lang.String)
	 */
	public Role getRoleById(int rolId) {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		Role role=session.getMapper(RoleDao.class).getRoleById(rolId);
		//通过角色id查找所有
		
		List<Fmenu> fmenus=fd.getFmenuByRole(rolId);
		
		List<Menu> menus=session.getMapper(MenuDao.class).getMenuByRoleId(rolId);
		//通过rolid查找所有的子菜单
		role.setFmenus(fmenus);
		role.setMenus(menus);
		MyBatisUtil.closeSession(session);
		return role;
	}
	public int editRoleToFmenu(int rolId,int i) {
		// TODO Auto-generated method stub
		//添加父菜单 与role的关系
		SqlSession session =MyBatisUtil.getSession();
		int count=session.getMapper(RoleDao.class).editRoleToFmenu(rolId,i);
		MyBatisUtil.closeSession(session);
		return count;
	}
	public int deleteRoleToFmenuAndMenu(int rolId) {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		if(session.getMapper(RoleDao.class).deleteFmenutoRole(rolId)>0){
			if(session.getMapper(RoleDao.class).deleteMenuToRole(rolId)>0){
				MyBatisUtil.closeSession(session);
				return 1;
			}
		}
		MyBatisUtil.closeSession(session);
		return 0;
	}
	public void addRoleToMenu(int rolId, int parseInt) {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		session.getMapper(RoleDao.class).addRoleToMenu(rolId,parseInt);
		MyBatisUtil.closeSession(session);
	}
	public int editRoleInfo(int rolId,String rolName, int rolState, String rolRemark) {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		int count=session.getMapper(RoleDao.class).editRoleInfo(rolId,rolName,rolState,rolRemark);
		MyBatisUtil.closeSession(session);
		return count;
	}
	public void editState(int id, int state) {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		session.getMapper(RoleDao.class).editState(id,state);
		MyBatisUtil.closeSession(session);
	}
	public int deleteById(int id) {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		int count=session.getMapper(RoleDao.class).deleteById(id);
		MyBatisUtil.closeSession(session);
		return count;
	}
	public List<Fmenu> getAllFmenuAndMenu() {
		// TODO Auto-generated method stub
		List<Fmenu> fmenus=fd.getAllFmenu();
		SqlSession session =MyBatisUtil.getSession();
		for (Fmenu fmenu : fmenus) {
			List<Menu> menus=session.getMapper(MenuDao.class).getMenuByFmenuId(fmenu.getFmeId());
			fmenu.setMenus(menus);
		}
		MyBatisUtil.closeSession(session);
		return fmenus;
	}
	public int addRoleInfo(String rolName, int rolState, String rolRemark) {
		// TODO Auto-generated method stub
		SqlSession session =MyBatisUtil.getSession();
		if(session.getMapper(MenuDao.class).addRoleInfo(rolName,rolState,rolRemark)>0){
			MyBatisUtil.closeSession(session);
			return session.getMapper(RoleDao.class).getMaxId();
		}
		MyBatisUtil.closeSession(session);
		//如果失败，则返回-1
		return -1;
	}
	public int getAllCount(SelectRoleInfo sri) {
		SqlSession session =MyBatisUtil.getSession();
		int count=session.getMapper(RoleDao.class).getAllCount(sri);
		MyBatisUtil.closeSession(session);
		return count;
	}
	public List<Role> getAllRoleBySelectInfo(SelectRoleInfo sri, PageInfo pi) {
		SqlSession session =MyBatisUtil.getSession();
		pi.setStartIndex();
		Map<String,Object> map=new HashMap<String,Object>();
		if(sri!=null){
			map.put("name", sri.getName());
			map.put("state",sri.getState());
		}
		if(pi!=null){
			map.put("startIndex", pi.getStartIndex());
			map.put("per", pi.getPer());
		}
		List<Role> rolelist=session.getMapper(RoleDao.class).getAllRoleBySelectInfo(map);
		MyBatisUtil.closeSession(session);
		return rolelist;
	}
	public int ajaxrolename(String rolname) {
		SqlSession session =MyBatisUtil.getSession();
		if(session.getMapper(RoleDao.class).ajaxrolename(rolname)>0){
			MyBatisUtil.closeSession(session);
			return 1;
		}
		MyBatisUtil.closeSession(session);
		return 0;
	}
	public String getRoleNameById(int id) {
		SqlSession session =MyBatisUtil.getSession();
		String result=session.getMapper(RoleDao.class).getRoleNameById(id);
		MyBatisUtil.closeSession(session);
		return result;
	}

}
